package com.mh.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mh.domain.Product;
import com.mh.domain.Product;

/**
 * DAO to manage Product entities.
 * 
 */
@Repository("ProductDAO")
@Transactional
public class ProductDAOImpl extends AbstractJpaDao<Product> implements
		ProductDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 * 
	 * @generated
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Product.class }));
	/**
	 * EntityManager injected by Spring for persistence unit mhtest
	 *
	 * 
	 * @generated
	 */
	@PersistenceContext(unitName = "mhtest")
	private EntityManager entityManager;

	/**
	 * Instantiates a new ProductDAOImpl
	 *
	 * 
	 * @generated
	 */
	public ProductDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 * 
	 * @generated
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 * 
	 * @generated
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findProductByNameContaining
	 *
	 */
	@Transactional
	public Set<Product> findProductByNameContaining(String name) throws DataAccessException {

		return findProductByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findProductByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByModifiedByContaining
	 *
	 */
	@Transactional
	public Set<Product> findProductByModifiedByContaining(String modifiedBy) throws DataAccessException {

		return findProductByModifiedByContaining(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findProductByModifiedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByModifiedByContaining(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByModifiedByContaining", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByPrice
	 *
	 */
	@Transactional
	public Set<Product> findProductByPrice(java.lang.Double price) throws DataAccessException {

		return findProductByPrice(price, -1, -1);
	}

	/**
	 * JPQL Query - findProductByPrice
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByPrice(java.lang.Double price, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByCreatedOn
	 *
	 */
	@Transactional
	public Set<Product> findProductByCreatedOn(java.util.Date createdOn) throws DataAccessException {

		return findProductByCreatedOn(createdOn, -1, -1);
	}

	/**
	 * JPQL Query - findProductByCreatedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByCreatedOn(java.util.Date createdOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByCreatedOn", startResult, maxRows, createdOn);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByName
	 *
	 */
	@Transactional
	public Set<Product> findProductByName(String name) throws DataAccessException {

		return findProductByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findProductByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByName", startResult, maxRows, name);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByModifiedBy
	 *
	 */
	@Transactional
	public Set<Product> findProductByModifiedBy(String modifiedBy) throws DataAccessException {

		return findProductByModifiedBy(modifiedBy, -1, -1);
	}

	/**
	 * JPQL Query - findProductByModifiedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByModifiedBy(String modifiedBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByModifiedBy", startResult, maxRows, modifiedBy);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByCreatedByContaining
	 *
	 */
	@Transactional
	public Set<Product> findProductByCreatedByContaining(String createdBy) throws DataAccessException {

		return findProductByCreatedByContaining(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findProductByCreatedByContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByCreatedByContaining(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByCreatedByContaining", startResult, maxRows, createdBy);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductById
	 *
	 */
	@Transactional
	public Product findProductById(Integer id) throws DataAccessException {

		return findProductById(id, -1, -1);
	}

	/**
	 * JPQL Query - findProductById
	 *
	 */

	@Transactional
	public Product findProductById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findProductById", startResult, maxRows, id);
			return (com.mh.domain.Product) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findProductByCreatedBy
	 *
	 */
	@Transactional
	public Set<Product> findProductByCreatedBy(String createdBy) throws DataAccessException {

		return findProductByCreatedBy(createdBy, -1, -1);
	}

	/**
	 * JPQL Query - findProductByCreatedBy
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByCreatedBy(String createdBy, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByCreatedBy", startResult, maxRows, createdBy);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByModifiedOn
	 *
	 */
	@Transactional
	public Set<Product> findProductByModifiedOn(java.util.Date modifiedOn) throws DataAccessException {

		return findProductByModifiedOn(modifiedOn, -1, -1);
	}

	/**
	 * JPQL Query - findProductByModifiedOn
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProductByModifiedOn(java.util.Date modifiedOn, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProductByModifiedOn", startResult, maxRows, modifiedOn);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * JPQL Query - findProductByPrimaryKey
	 *
	 */
	@Transactional
	public Product findProductByPrimaryKey(Integer id) throws DataAccessException {

		return findProductByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findProductByPrimaryKey
	 *
	 */

	@Transactional
	public Product findProductByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findProductByPrimaryKey", startResult, maxRows, id);
			return (com.mh.domain.Product) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllProducts
	 *
	 */
	@Transactional
	public Set<Product> findAllProducts() throws DataAccessException {

		return findAllProducts(-1, -1);
	}

	/**
	 * JPQL Query - findAllProducts
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findAllProducts(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllProducts", startResult, maxRows);
		return new LinkedHashSet<Product>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Product entity) {
		return true;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Product> findAllProducts(Product product, int startResult,
			int maxRows) {
		List lst = entityManager.createQuery("select myProduct from Product myProduct").getResultList();
		return (List<Product>)lst;
	}
	
	
	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Product> findProducts(Product product) throws DataAccessException {
		String queryFilter = "select myProduct from Product myProduct where 1=1 ";
		
		if (product.getId() != null && product.getId().toString() !=""){
			queryFilter += " and myproduct.id = " + product.getId().toString() ;
		} else {
			if (product.getName() != null && product.getName() !=""){
				queryFilter += " and upper(myproduct.name) like '%" + product.getName().toUpperCase() + "%'";
			}
			if (product.getPrice()  != null && product.getPrice().toString() !=""){
				queryFilter += " and myproduct.price =" + product.getPrice() + "";;
			}
			
			
		}
		
		return new LinkedHashSet<Product>(entityManager.createQuery(queryFilter).getResultList());
		
	}
	
	
}
